package hu.bme.tmit.futureinternet.simulator;

import hu.bme.tmit.futureinternet.log.Logger;
import hu.bme.tmit.futureinternet.model.User;

import java.io.FileWriter;
import java.io.IOException;
import java.text.MessageFormat;

import org.jgrapht.VertexFactory;
import org.jgrapht.generate.ScaleFreeGraphGenerator;
import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.SimpleWeightedGraph;

public class Main {

	public static final int SIZE_OF_COMMUNITY = 1000;
	public static final int SIZE_OF_SIMULATIONS = 20;
	public static final int SIZE_OF_SIMULATION_CASES = 100;
	public static final double PROBABILITY_OF_SPREADING = 0.5;
	public static int USER_ID = 0;

	public static final String SIMULATION_INPUT = "res/simu/input/";
	public static final String SIMULATION_OUTPUT = "res/simu/output/";
	public static final String GRAPH_IO = "res/graph/";
	public static final String SUFFIX = MessageFormat.format(
			"{0} simulations of {1} sim.cases in Community size {2}",
			Integer.toString(Main.SIZE_OF_SIMULATIONS),
			Integer.toString(Main.SIZE_OF_SIMULATION_CASES),
			Integer.toString(Main.SIZE_OF_COMMUNITY));

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		Logger log = new Logger(Main.class);
		log.start();

		SimpleWeightedGraph<User, DefaultWeightedEdge> community = new SimpleWeightedGraph<User, DefaultWeightedEdge>(
				DefaultWeightedEdge.class);

		ScaleFreeGraphGenerator<User, DefaultWeightedEdge> generator = new ScaleFreeGraphGenerator<User, DefaultWeightedEdge>(
				Main.SIZE_OF_COMMUNITY);

		long startOfGenerating = System.currentTimeMillis();
		generator.generateGraph(community, new VertexFactory<User>() {

			@Override
			public User createVertex() {
				return new User(USER_ID++);
			}

		}, null);

		try {
			FileWriter writer = new FileWriter(MessageFormat.format("{0}{1}",
					GRAPH_IO, SUFFIX));

			for (DefaultWeightedEdge e : community.edgeSet()) {
				writer.append(MessageFormat.format("{0} {1}\n",
						community.getEdgeSource(e), community.getEdgeTarget(e)));
			}

			writer.close();

		} catch (IOException e) {
			e.printStackTrace();
		}

		log.i(MessageFormat.format("Graph generated (~ {0} msec).",
				System.currentTimeMillis() - startOfGenerating));
		// Initializer.initGraph(community, GRAPH_IO + SIZE_OF_COMMUNITY +
		// "community");

		TrendSpreadingSimulator simulator = new TrendSpreadingSimulator(
				SIMULATION_INPUT);

		try {
			simulator.Simulate(community);
		} catch (IOException e) {
			e.printStackTrace();
		}

		/*
		 * TrendSpreadingSimulator simulator10 = new TrendSpreadingSimulator(
		 * "p_10"); TrendSpreadingSimulator simulator50 = new
		 * TrendSpreadingSimulator( "p_50"); TrendSpreadingSimulator simulator90
		 * = new TrendSpreadingSimulator( "p_90"); TrendSpreadingSimulator
		 * simulator100 = new TrendSpreadingSimulator( "p_100");
		 */

		log.end("End of Main (~ {0} msec).");
	}
}
